from bs4 import BeautifulSoup
from datetime import datetime

def parse_sina_news(html):
    """解析新浪财经新闻HTML"""
    soup = BeautifulSoup(html, 'html.parser')
    news_items = []
    
    for li in soup.select('.list_009 ul li'):
        link = li.find('a')
        if link:
            news_items.append({
                'title': link.text.strip(),
                'url': link['href'],
                'source': 'sina',
                'publish_time': datetime.strptime(li.span.text, '%Y-%m-%d %H:%M:%S') if li.span else datetime.now()
            })
    
    return news_items

def parse_xueqiu_news(json_data):
    """解析雪球热帖JSON"""
    news_items = []
    
    for item in json_data.get('data', {}).get('list', []):
        news_items.append({
            'title': item.get('title', item.get('description', '')),
            'content': item.get('text', ''),
            'url': f"https://xueqiu.com{item.get('target', '')}",
            'source': 'xueqiu',
            'publish_time': datetime.fromtimestamp(item.get('created_at', 0)/1000)
        })
    
    return news_items

def parse_pbc_news(html):
    """解析央行官网新闻HTML"""
    soup = BeautifulSoup(html, 'html.parser')
    news_items = []
    
    for li in soup.select('.newslist li'):
        link = li.find('a')
        if link:
            news_items.append({
                'title': link.text.strip(),
                'url': f"http://www.pbc.gov.cn{link['href']}",
                'source': 'pbc',
                'publish_time': datetime.strptime(li.span.text, '%Y-%m-%d') if li.span else datetime.now()
            })
    
    return news_items